package switchviz.graphic.signal;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jdom.Element;

import switchviz.core.ResourceManager;
import switchviz.gui.component.PopupMenu;

public class SignalConfigurationPanel extends JPanel implements ActionListener, ListSelectionListener, MouseListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4901906561427289228L;
	public static final String SIGNALNAME = "Signals";
	
	private SignalsConfigurationPersistence _persistenceHandler = null;
	private JList _listsig = null;
	private JPanel _sigconfspace = null;
	private IGaugeSignal _signal = null;
	private PopupMenu _popup = null;
	private int _oldindex = -1;
	private NewSignalDialog _sigdialog = null;
	
	public SignalConfigurationPanel(Element root)
	{
		_persistenceHandler = new SignalsConfigurationPersistence(root);
		initialize();
		reloadList();
	}
	
	private void reloadList()
	{
		ListSignalElement[] newlist = _persistenceHandler.getAllSignalDataModel();
		_oldindex = -1;
		if(newlist != null)
		{
			_listsig.setListData(newlist);
			_listsig.setEnabled(true);
			_listsig.setSelectedIndex(0);
		}
		else
		{
			_listsig.setListData(new String[]{""});
			_listsig.setEnabled(false);
			_sigconfspace.removeAll();
			this.validate();
			this.repaint();
		}
	}
	
	private void initialize()
	{
		this.setLayout(new BorderLayout());
		this.setName(SIGNALNAME);

		_listsig = new JList();
		_listsig.setPreferredSize(new Dimension(200, 100));
		_listsig.getSelectionModel().addListSelectionListener(this);
		_listsig.addMouseListener(this);
		_sigconfspace = new JPanel(new BorderLayout());
		
		_popup = new PopupMenu(new String[] {ResourceManager.getInstance().getActionAddSignal(), 
							ResourceManager.getInstance().getActionRemoveSignal()}, this);
	
		
		JPanel confsigpanel = new JPanel(new BorderLayout());
		confsigpanel.add(_listsig, BorderLayout.WEST);
		confsigpanel.add(_sigconfspace, BorderLayout.CENTER);
		
		this.add(confsigpanel, BorderLayout.CENTER);
	}

	public void actionPerformed(ActionEvent evt) 
	{
		String command = evt.getActionCommand();
		if(command.equals(ResourceManager.getInstance().getActionAddSignal()))
		{
			_sigdialog = NewSignalDialog.getInstance();
			_sigdialog.setName("New Signal");
			_sigdialog.addActionListener(this);
		}
		else if(command.equals(ResourceManager.getInstance().getActionRemoveSignal()))
		{
			if(_listsig.getSelectedIndex() >= 0)
			{
				_persistenceHandler.getElement().removeContent(_signal.getElement());
				reloadList();
			}
		}
		else if(command.equals(ResourceManager.getInstance().getActionAddItem()))
		{
			String name = _sigdialog.getSigName();
			if(name.length() == 0) return;
			
			for(int i = 0; i < _listsig.getModel().getSize(); ++i)
			{
				if(_listsig.getModel().getElementAt(i).toString().equals(name))
					name += "_1";
			}
			
			if (_sigdialog.getSigType().equals(BasicSignalDataModel.TYPETEST))
				_persistenceHandler.addSignal((new TestSignal(_persistenceHandler.getNewSignalNode(), name)).getElement());
			else if (_sigdialog.getSigType().equals(BasicSignalDataModel.TYPESIMPLE))
				_persistenceHandler.addSignal((new SimpleSignal(_persistenceHandler.getNewSignalNode(), name)).getElement());
			else if (_sigdialog.getSigType().equals(BasicSignalDataModel.TYPECOMPLEX))
				_persistenceHandler.addSignal((new ComplexSignal(_persistenceHandler.getNewSignalNode(), name)).getElement());
			reloadList();
			_sigdialog.setVisible(false);
		}
	}
	
	public Element getElement()
	{
		updateElement(_listsig.getSelectedIndex());
		return _persistenceHandler.getElement();
	}
	
	private void updateElement(int index)
	{
		if(index >= 0 && _signal != null && index < _listsig.getModel().getSize())
		{
			Element selected = _signal.getElement();
			((ListSignalElement)_listsig.getModel().getElementAt(index)).setElement(selected);
		}
	}

	public void valueChanged(ListSelectionEvent evt) 
	{
		int selval = ((ListSelectionModel)evt.getSource()).getMinSelectionIndex();
		if(selval >= 0)
		{
			updateElement(_oldindex);
			_oldindex = selval;
			ListSignalElement lselement = (ListSignalElement)_listsig.getModel().getElementAt(selval);
			if(lselement.getDatamodel().getType().equals(BasicSignalDataModel.TYPETEST))
			{
				_signal = new TestSignal(lselement.getElement());
				_sigconfspace.removeAll();
				_sigconfspace.add(new JScrollPane(_signal.getPanel()), BorderLayout.CENTER);
				this.validate();
				this.repaint();
			}
			else if(lselement.getDatamodel().getType().equals(BasicSignalDataModel.TYPESIMPLE))
			{
				_signal = new SimpleSignal(lselement.getElement());
				_sigconfspace.removeAll();
				_sigconfspace.add(new JScrollPane(_signal.getPanel()), BorderLayout.CENTER);
				this.validate();
				this.repaint();
			}
			else if(lselement.getDatamodel().getType().equals(BasicSignalDataModel.TYPECOMPLEX))
			{
				_signal = new ComplexSignal(lselement.getElement());
				
				for (int j=0; j<_persistenceHandler.getAllSignalDataModel().length; j++)
				{
					if (_signal.getName() != _persistenceHandler.getAllSignalDataModel()[j].getDatamodel().getName())
						((ComplexSignal)_signal).addSimpleSignal(_persistenceHandler.getAllSignalDataModel()[j].getDatamodel().getName());
					
				}
				
				_sigconfspace.removeAll();
				_sigconfspace.add(new JScrollPane(_signal.getPanel()), BorderLayout.CENTER);
				this.validate();
				this.repaint();
			}
		}
	}

	public void mousePressed(MouseEvent e) 
	{
		if (e.isPopupTrigger() || e.getButton() == 3) 
			_popup.show(e.getComponent(), e.getX(), e.getY());
	}
	
	public void mouseClicked(MouseEvent arg0) {}
	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mouseReleased(MouseEvent arg0) {}
}
